package com.zjmzxfzhl.modules.app.service.impl;

import com.zjmzxfzhl.modules.app.entity.*;
import com.zjmzxfzhl.modules.app.form.AlarmSetVo;
import com.zjmzxfzhl.modules.app.form.DocFilePath;
import com.zjmzxfzhl.modules.app.service.AppAlarmService;
import com.zjmzxfzhl.modules.app.service.AppAlarmUserService;
import com.zjmzxfzhl.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zjmzxfzhl.common.core.base.BaseServiceImpl;
import com.zjmzxfzhl.modules.app.mapper.AppAlarmSetMapper;
import com.zjmzxfzhl.modules.app.service.AppAlarmSetService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Service
 * 
 * @author 张森荣
 */
@Service
public class AppAlarmSetServiceImpl extends BaseServiceImpl<AppAlarmSetMapper, AppAlarmSet>
        implements AppAlarmSetService {
    @Autowired
    private AppAlarmService appAlarmService;
    @Autowired
    private AppAlarmUserService appAlarmUserService;

    @Override
    public IPage<AppAlarmSet> list(IPage<AppAlarmSet> page, AppAlarmSet appAlarmSet) {
        return page.setRecords(baseMapper.list(page, appAlarmSet));
    }
    @Override
    public  void addAlarmJob(){
        Date date=new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Integer weekIndex = cal.get(Calendar.DAY_OF_WEEK)-1;
        Integer monthIndex = cal.get(Calendar.DATE);

        AppAlarmSet appAlarmSet=new AppAlarmSet();
        appAlarmSet.setStatus("00");
        List<AppAlarmSet> alarmSetList=baseMapper.list(null,appAlarmSet);
        for (AppAlarmSet alarmSet:alarmSetList) {
            AlarmSetVo alarmSetVo=alarmSet.getAlarmSetVo();
            if (alarmSetVo.getType() != null && !"".equals(alarmSetVo.getType())) {
                switch (alarmSetVo.getType()) {
                    case "00": //每周提醒
                        if(weekIndex.equals(alarmSetVo.getWeekDay())){
                            try{
                                appAlarmService.addAlarm(alarmSet);
                            }catch (Exception e){
                                e.getMessage();
                            }
                        }
                        break;
                    case "01": //每月提醒
                        if(monthIndex.equals(alarmSetVo.getMonthDay())){
                            try{
                                appAlarmService.addAlarm(alarmSet);
                            }catch (Exception e){
                                e.getMessage();
                            }
                        }
                        break;
                    case "02": //仅提醒一次
                        if (date.getTime() >= alarmSetVo.getSetTime().getTime()) {
                            try{
                                appAlarmService.addAlarm(alarmSet);
                                //关闭提醒设置
                                alarmSet.setStatus("01");
                                this.updateById(alarmSet);
                            }catch (Exception e){
                                e.getMessage();
                            }
                        }
                        break;
                }
            }
        }
    }

    /**
     * 新增项目，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appAlarmSet
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAppAlarmSet(AppAlarmSet appAlarmSet) {
        //保存产品成员数组，标签数组
        List<SysUser> userList=null;
        List<DocFilePath> docFilePaths= null;
        if(appAlarmSet.getUserList()!=null){
            userList=appAlarmSet.getUserList();
        }
        //添加项目
        appAlarmSet =setAppAlarmSet(appAlarmSet);
        Boolean result = this.save(appAlarmSet);
        if(result==false){
            return result;
        }
        appAlarmSet=this.getById(appAlarmSet.getSetId());

        //添加项目成员，项目标签
        if(userList!=null){
            for (SysUser user:userList) {
                AppAlarmUser appAlarmUser=new AppAlarmUser();
                appAlarmUser.setSetId(appAlarmSet.getSetId());
                appAlarmUser.setUserId(user.getUserId());
                appAlarmUserService.saveAppAlarmUser(appAlarmUser);
            }
        }
        return result;
    }

    private AppAlarmSet setAppAlarmSet(AppAlarmSet appAlarmSet){

        return appAlarmSet;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppAlarmSet queryById(String id) {
        AppAlarmSet appAlarmSet=new AppAlarmSet();
        appAlarmSet.setSetId(id);
        List<AppAlarmSet> alarmSetList= baseMapper.list(null, appAlarmSet);
        if(alarmSetList.size()>0){
            return alarmSetList.get(0);
        }
        return null;
    }

    /**
     * 新增机构，自动计算机构级别、机构级次码、是否叶子
     *
     * @param appAlarmSet
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAppAlarmSet(AppAlarmSet appAlarmSet) {
        //保存修改标签数组
        List<SysUser> userList=null;
        if(appAlarmSet.getUserList()!=null){
            userList=appAlarmSet.getUserList();
        }

        appAlarmSet.setUpdateTime(new Date());
        Boolean result = this.updateById(appAlarmSet);
        if(result==false){
            return false;
        }

        //删除标签
        //添加标签
        if(userList!=null){
            AppAlarmUser appAlarmUser=new AppAlarmUser();
            appAlarmUser.setSetId(appAlarmSet.getSetId());
            appAlarmUserService.relationDel(appAlarmUser);
            for (SysUser user:userList) {
                appAlarmUser.setUserId(user.getUserId());
                appAlarmUserService.saveAppAlarmUser(appAlarmUser);
            }
        }
        return result;
    }
}
